'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 公会成员审核列表
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var unionGroupModel = require('../../../../models/unionGroupModel');
var PlayerManager = require('../../../../libs/playerManager');
var unionGroupConf = require('../../../../config/unionGroupConf');
var unionGroupService = require('../../../../services/unionGroupService');
var onlineService = require('../../../../services/onlineService.js');

/**
 * 公会成员审核列表
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var role_model;
    var union_id;
    var union_user_check_list;
    var union_group_model;
    var end_list = [];

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;
                union_id = role_model.getUnionId();

                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                union_group_model = model;
                async.forEach(union_group_model.apply_list, function (role_item, cb1) {
                    _removeUnionUserMsg(role_item, union_group_model, cb1);
                }, function (err) {
                    union_group_model.save(function (err) {
                        cb();
                    });
                });
            },
            function (cb) {
                _getUnionUserMsg1(union_group_model.apply_list, union_id, cb);
            },
            function (list, cb) {
                end_list = list;
                cb();
            }

        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    union_user_check_list: end_list
                }
            });
        }
    );
};

// 获取成员信息
var _getUnionUserMsg1 = function (union_group_list, union_id, callback) {
    var union_member_list = [];
    var item;
    async.forEach(union_group_list, function (union_user_role_id, cb) {
        _getUnionUserMsg(union_user_role_id, union_id, function (err, union_member) {
            if (!!err) {
                console.error(err);
                return cb();
            }
            if (!union_member) {
                union_member = [];
            }

            if (union_member.length > 0) {
                union_member_list.push(union_member[0]);
            }
            cb();
        });
    }, function (err) {
        if (!!err) {
            console.error(err);
        }
        callback(null, union_member_list);
    });
};

// 获取成员信息
var _getUnionUserMsg = function (role_id, union_id, callback) {
    var user_item_msg = [];
    var position = 3;
    var is_have_union = 0;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [

            function (cb) {
                player_manager.getModel(
                    ['role', 'accrued'],
                    cb
                );
            },
            function (cb) {
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                var union_group_model = model;
                var union_group_list = union_group_model.getUnionUserList();
                _.some(union_group_list, function (group_item) {
                    if (group_item.role_id == role_id) {
                        position = group_item.position;
                        return true;
                    }
                });
                cb();
            },
            function (cb) {
                // 判断是否玩家在线
                onlineService.checkRoleOnline(role_id, cb);
            },

            function (is_online, cb) {
                var role_model = player_manager.role;
                var union_id = role_model.union_id;
                if (!union_id) {
                    var user_is_online = 0;
                    if (!!is_online) {
                        user_is_online = 1;
                    } else {
                        user_is_online = 0;
                    }
                    user_item_msg.push({
                        role_id: role_id,
                        lv: role_model.getLv(),
                        vip: role_model.getVip(),
                        main_power: role_model.main_power,
                        name: role_model.name,
                        is_online: user_is_online,
                        position: position,
                        job_type: role_model.getJob(),
                        is_have_union: is_have_union
                    });
                }
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
            }
            callback(null, user_item_msg);
        }
    );
};

// 清空已加入联盟的成员审核信息
var _removeUnionUserMsg = function (role_id, union_group_model, callback) {
    var user_item_msg = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [

            function (cb) {
                player_manager.getModel(
                    ['role', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var union_id = role_model.union_id;
                if (!!union_id) {
                    // 移除审核列表中id
                    union_group_model.removeApplyId(role_id);
                }
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
            }
            callback();
        }
    );
};
